import $ from 'jquery'
export function toggle() {
  $("#k").on("click", function (e) {
    e.preventDefault();
    keysClick($(this));
  });
  $("#dark").on("click", function (e) {
    e.preventDefault();
    keysClick($(this), function () {
      //回调函数
      var isDark = localStorage.getItem("isDark");
      if (isDark == "true") {
        localStorage.setItem("isDark", "false");
      } else {
        localStorage.setItem("isDark", "true");
      }
      isDarkfun();
    });
  });
  //按钮开关
  function keysClick(obj, callback) {
    var keys = obj.children();
    var yuan = keys.children();
    console.log();
    if (yuan.css("left") == 0 + "px") {
      yuan.css("left", 0.52 + "rem");
      keys.css("backgroundColor", "#22fb35");
      //   left = 1;
    } else {
      yuan.css("left", "0rem");
      keys.css("backgroundColor", "#f2f2f8");
      //   left = 0;
    }
    // if (callback) {
    //     callback();
    // }
    callback && callback();
  }
  // 深色模式
  function isDarkfun() {
    var isDark = localStorage.getItem("isDark");
    if (isDark == "true") {
      $("#dark").attr("isDark", "true");
      setTheme();
    } else {
      localStorage.setItem("isDark", "false");
      $("#dark").attr("isDark", "false");
      setTheme2();
    }
  }
  //首次进入页面判断执行深色模式
  isDarkfun();
  //首次进入页面时判断深色模式是否开启，如开启就执行开关函数
  if (localStorage.getItem("isDark") == "true") {
    keysClick($("#dark"));
  }
  // 设置夜间样式
  function setTheme() {
    // 把背景色设置黑色、把文字设置为白色
    $("*").css("color", "#fff");
    $(
      'body,nav ul,header,select,.addbank,.addbank .info input[type="text"],.tel input[type="text"],footer,.form-control'
    ).css({
      "background-color": "#000",
    });
    $(
      "ul.c li,.search,.text ul li,.search input[type='text'],.username,footer .left,.weather"
    ).css({
      "background-color": "#333",
    });
    $("body").css("opacity", "0.8");
  }

  // 取消夜间样式
  function setTheme2() {
    $("*").css("color", "");
    $(
      "body,nav ul,header,select,ul.c li,.search,.text ul li,.search,.search input[type='text'],footer,.addbank,.addbank .info input[type='text'],.tel input[type='text'],.username,footer .left,.weather,.form-control"
    ).css({
      "background-color": "",
    });
    $("body").css("opacity", "");
  }
}